package blog.caidabao.service;

import blog.caidabao.mapper.ArticleMapper;
import blog.caidabao.mapper.IndexArticleMapper;
import blog.caidabao.pojo.Article;
import blog.caidabao.pojo.IndexArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IndexArticleMapper indexArticleMapper;

    /**
     * 通过文章id查询文章
     * @param id
     * @return
     */
    public Article queryArticleById(Integer id){
        Article article = new Article();
        article.setId(id);
        Article article1=this.articleMapper.selectByPrimaryKey(article);
        if(article1==null){
            return null;
        }
        return article1;
    }

    /**
     * 保存文章
     * @param article
     * @return
     */
    public Boolean saveArticle(Article article) {
        int result=this.articleMapper.insert(article);
        if (result==0){
            return false;
        }
        return true;
    }



    /**
     *查询所有文章
     * @return
     */
    public List<Article> articleAll(){
        return this.articleMapper.articleAll();
    }

    /**
     *通过id删除文章
     * @param id
     * @return
     */
    public Boolean articleDelete(Integer id){
        int resultNumber = this.articleMapper.deleteByPrimaryKey(id);
        if (resultNumber==1){
            return true;
        }
        return false;
    }

    /**
     * 博客前台首页查询文章
     * @return
     */
    public List<IndexArticle> indexArticles(String ids){
        return this.indexArticleMapper.indexArticle(ids);
    }

    /**
     *查询最新前8篇文章
     * @return
     */
    public List<Article> newestArticles() {
        return this.articleMapper.newestArticles();
    }

    public Integer getLastInsertId() {

        return this.articleMapper.getLastInsertId();
    }

    public List<IndexArticle> findArticleByCategoryId(Integer id) {
        return this.indexArticleMapper.findArticleByCategoryId(id);
    }

    /**
     * 根据主键更新文章
     * @param article
     * @return
     */
    public Boolean editArticle(Article article) {
        int result=this.articleMapper.updateByPrimaryKey(article);
        if (result==0){
            return false;
        }
        return true;
    }

    /**
     * 根据文章内容模糊搜索文章标题
     * @param searchContent
     * @return
     */
    public List<Article> search(String searchContent) {
        StringBuilder searchContent1=new StringBuilder();
        searchContent1.append("%");
        searchContent1.append(searchContent);
        searchContent1.append("%");
        return this.articleMapper.search(searchContent1.toString());
    }

    /**
     * 文章数量统计
     * @return
     */
    public Integer count() {
        return this.articleMapper.count();
    }

    /**
     * 通过页码查询文章
     * @param pageNum
     * @return
     */
    public List<Article> getArticleByPage(Integer pageNum) {
        Integer truePageNum=(pageNum-1)*7;
        return this.articleMapper.getArticleByPage(truePageNum);
    }

    public Map<String, Object> loadModel(Long id) {
        return new HashMap<>();
    }
}
